package com.e3.basdoc.region.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.region.client.RegionHelper;
import com.e3.basdoc.region.client.RegionRPCService;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.basdoc.region.model.RegionQC;
import com.e3.core.base.ServiceException;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;

/**
 * Description: <br>
 * 
 * @author: <a href="mailto:senton1101@126.com">TianXiangdong</a> 2010-8-6
 */
public class RegionRPCServiceImpl extends GwtRPCAction implements RegionRPCService {

	public RegionModel newRegion(final RegionModel regionModel) throws WebException {
		return new GwtRPCExecutor<RegionModel>() {

			@Override
			public RegionModel execute() throws ServiceException, Exception {
				return RegionServiceClient.getRegionService().newRegion(regionModel);
			}

		}.doExecute();
	}

	public List<RegionModel> getRegionsByCondition(final RegionQC regionQC) throws WebException {
		return new GwtRPCExecutor<List<RegionModel>>() {
			@Override
			public List<RegionModel> execute() throws ServiceException, Exception {
				List<RegionModel> regions = RegionServiceClient.getRegionQueryService().getPagingRegionModels(regionQC, 0, 50);
				if (regions == null || regions.isEmpty()) {
					return regions;
				}
				fillParentRegion(regions);
				return regions;
			}

		}.doExecute();
		
	}

	public RegionModel modifyRegion(final RegionModel regionModel) throws WebException {
		return new GwtRPCExecutor<RegionModel>() {

			@Override
			public RegionModel execute() throws ServiceException, Exception {
				return RegionServiceClient.getRegionService().modifyRegion(regionModel);
			}

		}.doExecute();
	}

	public void deleteRegions(final List<RegionModel> regionModels) throws WebException {
		new GwtRPCVoidExecutor() {

			@Override
			public void execute() throws ServiceException, Exception {
				RegionServiceClient.getRegionService().deleteRegions(regionModels);
			}

		}.doExecute();
	}

	public List<RegionModel> getRegionChildren(final RegionModel regionModel) throws WebException {
		return new GwtRPCExecutor<List<RegionModel>>() {

			@Override
			public List<RegionModel> execute() throws ServiceException, Exception {
				List<RegionModel> models = new ArrayList<RegionModel>();
				if (regionModel == null) {
					RegionModel model = RegionHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}

				RegionModel qModel = regionModel;
				if (RegionHelper.INSTANCE.isRootModel(qModel)) {
					qModel = null;
				}

				List<RegionModel> result = RegionServiceClient.getRegionQueryService().getRegionChildren(qModel);
				RegionHelper.INSTANCE.setRegionRelations(regionModel, result);
				return result;
			}

		}.doExecute();
	}

	public RegionModel getRegionByCode(final String code) throws WebException {
		return new GwtRPCExecutor<RegionModel>() {

			@Override
			public RegionModel execute() throws ServiceException, Exception {
				return RegionServiceClient.getRegionQueryService().getRegionModelByCode(code);
			}

		}.doExecute();
	}

	public RegionModel getRegionById(final String id) throws WebException {
		return new GwtRPCExecutor<RegionModel>() {

			@Override
			public RegionModel execute() throws ServiceException, Exception {
				return RegionServiceClient.getRegionQueryService().getRegionModelById(Long.parseLong(id));
			}

		}.doExecute();
	}

	private void fillParentRegion(List<RegionModel> regions) throws Exception {
		// 填充region的上级region
		List<Long> parentIds = new ArrayList<Long>(regions.size());
		for (RegionModel region : regions) {
			if (!RegionHelper.INSTANCE.isTopModel(region)) {
				Long pId = region.getParentId();
				if (!parentIds.contains(pId)) {
					parentIds.add(pId);
				}
			}
		}

		if (!parentIds.isEmpty()) {
			Long[] ids = new Long[parentIds.size()];
			parentIds.toArray(ids);
			List<RegionModel> parentRegions = RegionServiceClient.getRegionQueryService().getRegionModelByIds(ids);
			// 设置查询结果中各region的上级region
			for (RegionModel region : regions) {
				Long pID = region.getParentId();
				for (RegionModel regionModel : parentRegions) {
					Long id = regionModel.getId();
					if (pID.equals(id)) {
						region.setParentModel(regionModel);
						break;
					}
				}
			}
		}
	}

	public PagingLoadResult<RegionModel> getPagingRegions(final PagingLoadConfig config) throws WebException {
		final RegionQC qc = config.get("LOAD_CONDTION");
		List<RegionModel> models = new GwtRPCExecutor<List<RegionModel>>() {
			int start = config.getOffset();
			int max = config.getLimit();
			@Override
			public List<RegionModel> execute() throws ServiceException,	Exception {
				List<RegionModel> regions = RegionServiceClient.getRegionQueryService().getPagingRegionModels(qc, start, max);
				if (regions == null || regions.isEmpty()) {
					return regions;
				}
				fillParentRegion(regions);
				return regions;
			}
			
		}.doExecute();
		if(models == null||models.isEmpty()){
			return new BasePagingLoadResult<RegionModel>(new ArrayList<RegionModel>());
		}
		
		long size = new GwtRPCExecutor<Long>(){
			@Override
			public Long execute() throws ServiceException, Exception {
				return RegionServiceClient.getRegionQueryService().getRegionSizeByCondition(qc);
			}
			
		}.doExecute();
		return new BasePagingLoadResult<RegionModel>(models, config.getOffset(), (int)size);
	}

}
